﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess.Accessors.Interfaces;
using DataAccess.DAO;
using NHibernate.Linq;

namespace DataAccess.Accessors
{
    public class UserAccountAccessor : IUserAccountAccessor
    {
        private readonly IDataAccessor _dataAccessor;

        public UserAccountAccessor(IDataAccessor dataAccessor)
        {
            _dataAccessor = dataAccessor;
        }

        public List<UserAccountDAO> GetUserAccountForUser(long userId)
        {
            using (var session = _dataAccessor.OpenNHibernateSession())
            {
                return (from userAccounts in session.Query<UserAccountDAO>()
                        where userAccounts.UserId == userId
                        select userAccounts).ToList();
            }
        }

        public void SaveUserAccount(UserAccountDAO account)
        {
            using (var session = _dataAccessor.OpenNHibernateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(account);
                    transaction.Commit();
                }
            }
        }
    }
}
